using B.S.Data.MES.API.Applications.WorkOrderManagement.Commands;
using B.S.Domain.Ticket;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.WorkOrderManagement.CommandHandlers
{
    /// <summary>
    /// 根据ID查询工单命令处理器
    /// </summary>
    public class GetWorkOrderByIdCommandHandler : IRequestHandler<GetWorkOrderByIdCommand, APIRequst<WorkOrder>>
    {
        private readonly IBaseRepository<WorkOrder> _workorderRepository;

        public GetWorkOrderByIdCommandHandler(IBaseRepository<WorkOrder> workorderRepository)
        {
            _workorderRepository = workorderRepository;
        }

        /// <summary>
        /// 处理根据ID查询工单的请求
        /// </summary>
        /// <param name="request">查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>工单信息</returns>
        public async Task<APIRequst<WorkOrder>> Handle(GetWorkOrderByIdCommand request, CancellationToken cancellationToken)
        {
            var workOrder = _workorderRepository.GetAll().FirstOrDefault(x => x.WorkId == request.WorkId && !x.isDel);
            
            if (workOrder == null)
            {
                return new APIRequst<WorkOrder>
                {
                    Code = RequstCode.失败,
                    Msg = "未找到指定的工单信息",
                    Data = null
                };
            }
            
            return new APIRequst<WorkOrder>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data = workOrder
            };
        }
    }
}